#region File Header

//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------

#endregion File Header

using System;
using System.Web.Caching;
using System.Web.UI.WebControls;
using AirWeb.ApplicationTools;

namespace FlashBack.Web
{
	/// <summary>
	///    Summary description for PicDirTree.
	/// </summary>
	public partial class PicDirTree : PageBase
	{
		public bool Autoselect; // true if the tree has to autoselect its first element
		protected Label IfEmptyText;
		protected bool m_cacheModel = false;

		public PicDirTree()
		{
			Page.Init += new EventHandler(Page_Init);
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			Initialze();
		}

		protected virtual void Initialze()
		{
			if (m_cacheModel)
				MyTree.TreeModel = GetTreeModel();
			else
				MyTree.TreeModel = CreateTreeModel();

			// Hide the tree label in most cases. Only Netscape uses it for now
			string showheader = Request.QueryString["ShowHeader"];
			if (showheader != null && showheader == "yes")
			{
				Header.Visible = true;
			}
			else
			{
				Header.Visible = false;
			}
		}

		protected virtual DirectoryTreeModel GetTreeModel()
		{
			// Set the filter (if any)
			string filter = Request.QueryString["FilterByDate"];

			if (CurrentUser != null)
			{
				// Use both the role name and the filter to construct a unique cache key;
				string key = CurrentUser.UserRole.Name + "_" + filter;

				DirectoryTreeModel o = (DirectoryTreeModel) Cache[key];
				if (o != null)
					return o;
				else
				{
					DirectoryTreeModel m = CreateTreeModel();
					Cache.Add(key, m, null, DateTime.MaxValue, new TimeSpan(0, 20, 0), CacheItemPriority.High, null);
					return m;
				}
			}

			return null;
		}

		protected virtual DirectoryTreeModel CreateTreeModel()
		{
			DirectoryTreeModel m = new DirectoryTreeModel(CurrentUser, Context);
			DirectoryHelper d = new DirectoryHelper();

			string rpath = Server.MapPath(d.CreateOriginalPath("\\"));
			m.SetPath(rpath);

			MyTree.IfEmptyText = "There are no pictures available.";

			return m;
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP+ Windows Form Designer.
			//
			InitializeComponent();
		}

		/// <summary>
		///    Required method for Designer support - do not modify
		///    the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
		}
	}
}